package com.tengju.data.infrastructure.repository.clickhouse.crm;

import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.clickhouse.crm.CrmActivityAnalysisRepository;
import com.tengju.data.domain.model.cqrs.CrmActivityAnalysisQO;
import com.tengju.data.domain.model.cqrs.CrmActivityCompleteValueQO;
import com.tengju.data.domain.shared.BasePage;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.dealerclickhouse.crm.ActivityAnalysisMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/12/29
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CrmActivityAnalysisRepositoryImpl implements CrmActivityAnalysisRepository {

    private final ActivityAnalysisMapper activityAnalysisMapper;

    @Override
    public List<Row> secTargetReachTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.secTargetReachTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> directorTargetReachTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.directorTargetReachTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> dealerTargetReachTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.dealerTargetReachTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> serviceProviderTargetReachTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.serviceProviderTargetReachTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> transformFunnel(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.transformFunnel(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> activityTransformFunnel(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.activityTransformFunnel(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> departmentRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.departmentRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> secRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.secRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> directorRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.directorRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> activityCompleteTotal(CrmActivityCompleteValueQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.activityCompleteTotal(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> addcartGoodsRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.addcartGoodsRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> moneyoutRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.moneyoutRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> dealerRankTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.dealerRankTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> overview(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.overview(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> overviewTable(CrmActivityAnalysisQO qo) {
        if (qo == null) {
            return Collections.emptyList();
        }
        List<Map<String,Object>> result = activityAnalysisMapper.overviewTable(qo);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<Row> subjectNameList(Long activityId, Integer activityType) {
        List<Map<String, Object>> result = activityAnalysisMapper.getSubjectNameList(activityId, activityType);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream()
                .map(Row::new)
                .collect(Collectors.toList());
    }

    @Override
    public int countSecTargetReachTable(CrmActivityAnalysisQO qo) {
        return activityAnalysisMapper.countSecTargetReachTable(qo);
    }

    @Override
    public int countServiceProviderTargetReachTable(CrmActivityAnalysisQO qo) {
        return activityAnalysisMapper.countServiceProviderTargetReachTable(qo);
    }

    @Override
    public int countDealerTargetReachTable(CrmActivityAnalysisQO qo) {
        return activityAnalysisMapper.countDealerTargetReachTable(qo);
    }

    @Override
    public int countDirectorTargetReachTable(CrmActivityAnalysisQO qo) {
        return activityAnalysisMapper.countDirectorTargetReachTable(qo);
    }
}
